package Controlador;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import Modelo.Estudiante;
import Modelo.Seccion;
import Vista.Vestudiante;


public class ControEstudiante implements ActionListener {
	
	private Vestudiante visest; 
	
	private	Estudiante estudiante;
	public static Seccion seccion = new Seccion();
	
	public ControEstudiante(Vestudiante visest){
		
		this.visest =visest;
		estudiante = new Estudiante();
	

}

	public void actionPerformed(ActionEvent ac) {
		
	
	
	if (ac.getSource() == visest.btnIns) {
		
		estudiante = new Estudiante( visest.txtced.getText(),visest.txtnombre.getText(),visest.txtapellido.getText());
		
			Estudiante estudiante=new Estudiante();
			estudiante.setCedula(visest.txtced.getText());
			estudiante.setNombre(visest.txtnombre.getText());
			estudiante.setApellido(visest.txtapellido.getText());
			seccion.Inscribirestudiante(estudiante);
			JOptionPane.showMessageDialog(null, "Estudiante Inscrito", 
					"Informacion:", 
					JOptionPane.INFORMATION_MESSAGE);	
			visest.btnbuscar.setEnabled(true);
			borrar();
}
	if (ac.getSource()== visest.btnbuscar)
	{	
		String cedula;

			if(seccion.Existenciaest(visest.txtced.getText())){
				cedula = visest.txtced.getText();
				seccion.Buscarestudiante(cedula);
				borrar();
				visest.txtced.setText(estudiante.getNombre());
				visest.txtnombre.setText(estudiante.getApellido());
				visest.txtapellido.setText(estudiante.getCedula());
				visest.btnret.setEnabled(true);
				visest.btnbuscar.setEnabled(false);
				
			} 
			else{
				visest.btnIns.setEnabled(true);
				visest.txtnombre.setEditable(true);
				visest.txtapellido.setEditable(true);
				JOptionPane.showMessageDialog(null, "Estudiante no registrado", 
						"Informacion:", 
						JOptionPane.INFORMATION_MESSAGE);}
		}

	
	
	
	
	
	if (ac.getSource()== visest.btnret)
	{
		String cedula;
		if (!visest.txtced.getText().equals("")) {
			if(seccion.Existenciaest(visest.txtced.getText())){
				
				cedula = visest.txtced.getText();
				seccion.Retirarestudiante(cedula);
				JOptionPane.showMessageDialog(null, "El Estudiante retirado satisfactoriamente", 
						"Informacion:", 
						JOptionPane.INFORMATION_MESSAGE);
		
	          }
		}
	}
}
	private void borrar() {
		visest.txtced.setText("");
		visest.txtnombre.setText("");
		visest.txtapellido.setText("");
		visest.txtced.requestFocus();
	}


}

	